package org.ielts.service;

import java.util.Date;

import org.ielts.domain.LearnStatus;
import org.ielts.domain.base.Course;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.ChangeSignCourseLog;
import org.ielts.domain.log.ChangeSignCourseType;
import org.ielts.domain.log.ExtendSignCourseLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.report.StudentQuery;
import org.ielts.domain.security.User;
import org.ielts.repository.ChangeSignCourseLogRepository;
import org.ielts.repository.ExtendSignCourseLogRepository;
import org.ielts.repository.SignCourseLogRepository;
import org.ielts.repository.SignCourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yifeng.ProfileContext;
import com.yifeng.profile.Profile;

@Service
public class SignCourseService {

	@Autowired
	SignCourseRepository signCourseRepository;

	@Autowired
	SignCourseLogRepository signCourseLogRepository;

	public SignCourse signUp(String studentId, String courseId, String contractId, String cardId, String lessonCard,
			String title, int times, Date beginTime, Date endTime, Date signTime, String consultantId, String book) {

		User creator = (User) Profile.getInstance().getUser();

		SignCourseLog signCourseLog = new SignCourseLog();
		signCourseLog.setBeginTime(beginTime);
		signCourseLog.setTitle(title);
		signCourseLog.setCardId(cardId);
		signCourseLog.setChecked(true);
		signCourseLog.setChecker(creator);
		signCourseLog.setConsultant(null);
		signCourseLog.setContractId(contractId);
		signCourseLog.setLogTime(signTime);
		signCourseLog.setBook(book);

		if (consultantId != null) {
			User consultant = new User();
			consultant.setId(consultantId);
			signCourseLog.setConsultant(consultant);
		}

		Course course = new Course();
		course.setId(courseId);
		signCourseLog.setCourse(course);

		signCourseLog.setCreator(creator);
		signCourseLog.setCreateTime(new Date());
		signCourseLog.setEndTime(endTime);
		signCourseLog.setLessonCard(lessonCard);

		Student student = new Student();
		student.setId(studentId);
		signCourseLog.setStudent(student);

		signCourseLog.setTimes(times);
		signCourseLog.setTitle(title);

		SignCourse signCourse = new SignCourse();

		signCourse.setTitle(title);
		signCourse.setStudent(student);

		signCourse.setCourse(course);

		signCourse.setContractId(contractId);
		signCourse.setLessonCard(lessonCard);

		signCourse.setCardId(cardId);
		signCourse.setTimes(times);
		signCourse.setBeginTime(beginTime);
		signCourse.setEndTime(endTime);

		signCourse = signCourseRepository.save(signCourse);

		signCourseLog.setSignCourse(signCourse);
		signCourseLog.setChecked(true);

		signCourseLogRepository.save(signCourseLog);

		return signCourse;
	}

	@Transactional
	public SignCourse modifySignCourse(String id, String courseId, String contractId, String cardId, String term,
			String lessonCard, int times, Date beginTime, Date endTime) {
		SignCourse signCourse = signCourseRepository.findOne(id);
		signCourse.setBeginTime(beginTime);
		signCourse.setCardId(cardId);
		signCourse.setContractId(contractId);

		Course course = new Course();
		course.setId(courseId);
		signCourse.setCourse(course);
		signCourse.setLessonCard(lessonCard);
		signCourse.setEndTime(endTime);
		signCourse.setTerm(term);

		signCourse.setTimes(times);

		return signCourse;
	}

	@Transactional
	public SignCourse findOne(String id) {
		SignCourse signCourse = signCourseRepository.findOne(id);

		return signCourse;
	}

	@Autowired
	ExtendSignCourseLogRepository extendSignCourseLogRepository;

	@Transactional
	public ExtendSignCourseLog extendSignCourse(String id, String contractId, String cardId, Date endTime, int times,
			String consultant, String title) {
		User creator = (User) Profile.getInstance().getUser();

		SignCourse signCourse = signCourseRepository.findOne(id);

		ExtendSignCourseLog extendSignCourseLog = new ExtendSignCourseLog();

		extendSignCourseLog.setSignCourse(signCourse);
		extendSignCourseLog.setTitle(title);
		extendSignCourseLog.setCreateTime(new Date());

		User user = new User();
		user.setId(consultant);
		extendSignCourseLog.setConsultant(user);

		extendSignCourseLog.setCreator(creator);
		extendSignCourseLog.setNewCardId(cardId);
		extendSignCourseLog.setNewContract(contractId);
		extendSignCourseLog.setExtendTimes(times);
		extendSignCourseLog.setExtendDate(endTime);

		extendSignCourseLog.setChecked(true);
		extendSignCourseLog.setChecker(creator);

		extendSignCourseLog.setStudent(signCourse.getStudent());

		signCourse.setEndTime(endTime);
		signCourse.setTimes(signCourse.getTimes() + times);

		extendSignCourseLogRepository.save(extendSignCourseLog);

		return extendSignCourseLog;
	}

	@Autowired
	ChangeSignCourseLogRepository changeSignCourseLogRepository;

	@Transactional
	public ChangeSignCourseLog changeSignCourse(String id, Date endTime, int times, ChangeSignCourseType type,
			String title) {
		User creator = (User) Profile.getInstance().getUser();

		SignCourse signCourse = signCourseRepository.findOne(id);

		ChangeSignCourseLog changeSignCourseLog = new ChangeSignCourseLog();

		changeSignCourseLog.setType(type);

		changeSignCourseLog.setSignCourse(signCourse);
		changeSignCourseLog.setCreateTime(new Date());

		changeSignCourseLog.setCreator(creator);

		changeSignCourseLog.setNewEndDate(endTime);
		changeSignCourseLog.setOldEndDate(signCourse.getEndTime());
		changeSignCourseLog.setNewTimes(times);
		changeSignCourseLog.setOldTimes(signCourse.getTimes());

		changeSignCourseLog.setStudent(signCourse.getStudent());
		changeSignCourseLog.setTitle(title);

		signCourse.setEndTime(endTime);
		signCourse.setTimes(times);

		changeSignCourseLogRepository.save(changeSignCourseLog);

		return changeSignCourseLog;
	}

	@Transactional
	public SignCourse checkSign(SignCourseLog courseLog, boolean checked) {
		SignCourse signCourse = new SignCourse();
		signCourse.setBeginTime(courseLog.getBeginTime());
		signCourse.setBook(courseLog.getBook());
		signCourse.setCardId(courseLog.getCardId());
		signCourse.setContractId(courseLog.getContractId());
		signCourse.setCourse(courseLog.getCourse());
		signCourse.setEndTime(courseLog.getEndTime());
		signCourse.setLearnStatus(LearnStatus.Learning);
		signCourse.setName(courseLog.getName());
		signCourse.setStudent(courseLog.getStudent());
		signCourse.setTerm(courseLog.getTerm());
		signCourse.setTimes(courseLog.getTimes());
		signCourse.setTitle(courseLog.getTitle());
		signCourse.setNote(courseLog.getNote());
		signCourse.setLessonCard(courseLog.getLessonCard());

		User checker = (User) Profile.getInstance().getUser();

		signCourse = signCourseRepository.save(signCourse);

		courseLog.setChecked(checked);
		courseLog.setSignCourse(signCourse);
		courseLog.setChecker(checker);

		// ERROR
		signCourseLogRepository.save(courseLog);

		return signCourse;
	}

	@Transactional
	public void save(SignCourse signCourse) {
		signCourseRepository.save(signCourse);
	}

	@Transactional
	public Page<SignCourse> findByRemainTimes(StudentQuery studentQuery, long remainTimes, Pageable pageable) {
		Page<SignCourse> signCourses = signCourseRepository.findByRemainTimes(studentQuery, remainTimes, pageable);
		return signCourses;
	}

	@Transactional
	public Page<SignCourse> findByRemainDate(StudentQuery studentQuery, Date endTime, Pageable pageable) {
		Page<SignCourse> signCourses = signCourseRepository.findByRemainDate(studentQuery, endTime, pageable);
		return signCourses;
	}
}
